package kr.hkit.androidcomplete01.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ListAddDelMulti extends Activity {
	ArrayList<String> items;
	ArrayAdapter<String> adapter;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddelsinglechoice);
		
		items = new ArrayList<String>();
		items.add("First");
		items.add("Second");
		items.add("Third");
		items.add("Fourth");
		items.add("Fifth");
		
		adapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_multiple_choice, items);
		list = (ListView) findViewById(R.id.adlist);
		list.setAdapter(adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}
	
	public void mOnClick(View v){
		EditText ed = (EditText) findViewById(R.id.newitem);
		switch(v.getId()){
		case R.id.add:
			String text = ed.getText().toString();
			if(text.length() != 0){
				items.add(text);
				ed.setText("");
				adapter.notifyDataSetChanged();
			}
			break;
		case R.id.delete:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			if(sb.size() != 0){
				for(int i = list.getCount() - 1; i>=0; i--){
					if(sb.get(i)){
						items.remove(i);
					}
				}
				list.clearChoices();
				adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
